package javaPersistence;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import beans.ProductosBean;
import beans.UsuarioBean;

public class User {
	
	public boolean verifyUser(String user, String pass){
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder("SELECT * FROM USUARIO WHERE CORREOELECTRONICO = '");
			query.append(user).append("' AND CONTRASENA = '").append(pass).append("'");
			System.out.println("query: "+query.toString());
			ResultSet resultSet = dbConSoft.getDeclaracion().executeQuery(query.toString());
			while (resultSet.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean insertUser(int code, String name, String lastName, String email, String pass){
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder("INSERT INTO USUARIO VALUES (");
			query.append(code).append(",'").append(name).append("','").append(lastName).append("','").append(pass).append("','").append(email).append("')");
			System.out.println("query: "+query.toString());
			if(dbConSoft.getDeclaracion().executeUpdate(query.toString()) > 0)
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public Integer obtainUser(String user){
		Integer code = null;
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder("SELECT idUsuario FROM USUARIO WHERE CORREOELECTRONICO = '");
			query.append(user).append("'");
			System.out.println("query: "+query.toString());
			ResultSet resultSet = dbConSoft.getDeclaracion().executeQuery(query.toString());
			while (resultSet.next()) {
				code = resultSet.getInt("idUsuario");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return code;
	}
	
	public ArrayList<UsuarioBean> loadAllUsers(){
		ArrayList<UsuarioBean> arrUser = new ArrayList<UsuarioBean>();
		try {
			UsuarioBean userB;
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder();
			query.append("SELECT * FROM USUARIO");
			ResultSet resultSet = dbConSoft.getDeclaracion().executeQuery(query.toString());
			while (resultSet.next()) {
				userB = new UsuarioBean();
				userB.setIdUsuario(resultSet.getInt(""));
				userB.setNombre(resultSet.getString(""));
				userB.setApellido(resultSet.getString(""));
				userB.setContrasena(resultSet.getString(""));
				userB.setCorreoElectronico(resultSet.getString(""));
				arrUser.add(userB);
			}
		} catch (SQLException e) {
		}
		return arrUser;
	}

}
